package com.wyx.videoplay;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.wyx.videoplay.listener.OnControlClickListener;
import com.wyx.videoplay.listener.OnMediaListener;
import com.wyx.videoplay.player.NurVideoView;
import com.wyx.videoplay.utils.SharePreferenceUtil;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class RecordActivity extends AppCompatActivity {


    private NurVideoView nurVideoPlayer;
    private ImageView mRecordView;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    Toast.makeText(RecordActivity.this, "截图成功", Toast.LENGTH_SHORT).show();

                    break;
                case 2:
                    Toast.makeText(RecordActivity.this, "开始录像", Toast.LENGTH_SHORT).show();
                    mRecordView.setImageResource(R.mipmap.record_pressed);
                    break;
                case 3:
                    Toast.makeText(RecordActivity.this, "结束录像", Toast.LENGTH_SHORT).show();
                    mRecordView.setImageResource(R.mipmap.record);

                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);

        String url = "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov";
//        String url = "/sdcard/MyMovies/2021-04-15 14:15:32.mp4";
        nurVideoPlayer = findViewById(R.id.videoView);
        TextView biaoqing = findViewById(R.id.biaoqing);
        TextView gaoqing = findViewById(R.id.gaoqing);
        biaoqing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nurVideoPlayer.setUp(RecordActivity.this, url, "我是标题:哇哈哈");
            }
        });
        gaoqing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nurVideoPlayer.setUp(RecordActivity.this, "http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8", "我是标题:营养快线");
            }
        });


        nurVideoPlayer.setUp(RecordActivity.this, url, "我是标题:农夫山泉");
        requestPermission();
        nurVideoPlayer.setOnControlClickListener(new OnControlClickListener() {
            @Override
            public void onBackBtnClick() {

            }

            @Override
            public void onVolumeControlClick() {

            }

            @Override
            public void onScreenControlClick() {

            }
        });
        nurVideoPlayer.setOnMediaListener(new OnMediaListener() {
            @Override
            public void onStart() {
                Log.e("TAG", "===OnMedia===" + "onStart");

            }

            @Override
            public void onPause() {
                Log.e("TAG", "===OnMedia===" + "onPause");

            }

            @Override
            public void onProgress(int progress, int duration) {
//                Log.e("TAG", "===OnMedia===" + "onProgress");   //类似于loading

            }

            @Override
            public void onChangeScreen(boolean isPortrait) {
                Log.e("TAG", "===OnMedia===" + "onChangeScreen");

            }

            @Override
            public void onEndPlay() {
                Log.e("TAG", "===OnMedia===" + "onEndPlay");

            }

            @Override
            public void onError() {
//                Toast.makeText(mContext, "网路未连接，请检查网络设置", Toast.LENGTH_SHORT).show();
                Log.e("TAG", "===OnMedia=onError==" + "网路未连接，请检查网络设置");

            }

            @Override
            public void goToPictures() {
                Log.e("TAG", "===OnMedia===" + "goToPictures");

            }

            @Override
            public void openEasyPusher(ImageView mTView, String statue) {
                Log.e("TAG", "===openEasyPusher===" + statue);
            }

            @Override
            public void closeEasyPusher(ImageView mTView, String statue) {
                Log.e("TAG", "===closeEasyPusher===" + statue);
            }

            @Override
            public void switchUrlType(ImageView mTView) {
                Log.e("TAG", "===OnMedia===" + "switchUrlType");

            }

            @Override
            public void getFrame(boolean currentFrame) {
                Log.e("TAG", "===OnMedia===" + "getFrame");
                mHandler.sendEmptyMessage(1);

            }

            @Override
            public void getStartRecord(ImageView mTView, String statue) {
                mRecordView = mTView;
                Log.e("TAG", "===OnMedia===" + "getStartRecord" + "=====" + statue);
                mHandler.sendEmptyMessage(2);

            }

            @Override
            public void getEndRecord(ImageView mTView, String statue) {
                mRecordView = mTView;
                Log.e("TAG", "===OnMedia===" + "getEndRecord" + "=====" + statue);
                mHandler.sendEmptyMessage(3);


            }

            @Override
            public void mVideoViewOK() {
                Log.e("TAG", "===OnMedia===" + "mVideoViewOK");

            }
        });


    }


    @Override
    public void onBackPressed() {
        if (nurVideoPlayer.getIsFullScreen()) {
            nurVideoPlayer.setChangeScreen(false);
        } else
            super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        nurVideoPlayer.pause();
        String recordType = (String) SharePreferenceUtil.get(this, SharePreferenceUtil.Key_Record, "end");
        if (recordType.equals("start")) {
            nurVideoPlayer.getEndRecord();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        nurVideoPlayer.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        nurVideoPlayer.stopPlay();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean b = nurVideoPlayer.onKeyDown(keyCode);
        return b || super.onKeyDown(keyCode, event);
    }

    private void requestPermission() {


        XXPermissions.with(this)
                // 不适配 Android 11 可以这样写
                //.permission(Permission.Group.STORAGE)
                // 适配 Android 11 需要这样写，这里无需再写 Permission.Group.STORAGE
                .permission(Permission.MANAGE_EXTERNAL_STORAGE)
                .request(new OnPermissionCallback() {

                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            Toast.makeText(RecordActivity.this, "获取存储权限成功", Toast.LENGTH_SHORT).show();

                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            Toast.makeText(RecordActivity.this, "被永久拒绝授权，请手动授予存储权限", Toast.LENGTH_SHORT).show();

                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.startPermissionActivity(RecordActivity.this, permissions);
                        } else {
                            Toast.makeText(RecordActivity.this, "获取存储权限失败", Toast.LENGTH_SHORT).show();

                        }
                    }
                });


    }



}
